﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Drawing;
using FarPoint.Web.Spread;
using DQYT.HTDZ.OracleDAL;

namespace SkWeb.FYCS
{
    public partial class TB_DJSKCBDReport : System.Web.UI.Page
    {
        #region 变量
        // 头行数
        int mTouRowNum;

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            #region
            try
            {
                lblMsg.Text = "";
                if (!IsPostBack)
                {
                    lblPath.Text = "单井射孔成本";
                    txtSHRQ.Text = DateTime.Today.ToString("yyyy-MM-dd");

                    TongJi();
                    BindData();

                }
            }
            catch (Exception ex)
            {
                lblMsg.Text = "错误信息:" + ex.Message;
            }
            #endregion
        }

        protected void btnTJ_Click(object sender, EventArgs e)
        {
            #region 统计
            try
            {
                lblMsg.Text = "";
                TongJi();
                BindData();
            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }

            #endregion
        }

        protected void btnDY_Click(object sender, EventArgs e)
        {
            #region 打印

            try
            {
                lblMsg.Text = "";
                //Module1.pi.Orientation = FarPoint.Win.Spread.PrintOrientation.Landscape;//横向
                //System.Drawing.Printing.PaperSize psize = new System.Drawing.Printing.PaperSize("A3", 1169, 1654);//297, 420毫米单位需要转换为英寸//1169,1654
                //Module1.pi.PaperSize = psize;

                //Module1.pi.PrintType = FarPoint.Win.Spread.PrintType.All;
                //Module1.pi.ShowColumnHeaders = false;           //显示列头
                //Module1.pi.ShowRowHeaders = false;              //显示行头
                //Module1.pi.ShowGrid = true;                     //显示网格
                //Module1.pi.ShowBorder = true;                   //显示边框
                //Module1.pi.ShowShadows = false;
                //Module1.pi.Preview = true;                    //打印预览

                //fpSpread1.Sheets[0].PrintInfo = Module1.pi;
                //fpSpread1.PrintSheet(0);

            }
            catch (Exception ex)
            {
                lblMsg.Text = ex.Message;
            }

            #endregion
        }

        /// <summary>
        /// 统计
        /// </summary>
        private void TongJi()
        {
            #region

            string strDate = txtSHRQ.Text.Trim();
            lblMsg.Text = "";
            mTouRowNum = 2;

            //初始化布局
            InitSpread(strDate);

            #endregion

        }

        /// <summary>
        /// 初始化布局
        /// </summary>
        /// <param name="RiQi"></param>
        public void InitSpread(string RiQi)
        {
            #region 正文

            fpSpread1.Sheets[0].ColumnHeader.DefaultStyle.Font.Name = "Verdana";
            fpSpread1.Sheets[0].ColumnHeader.DefaultStyle.Font.Size = 8;
            fpSpread1.Sheets[0].RowHeader.DefaultStyle.Font.Name = "Verdana";
            fpSpread1.Sheets[0].RowHeader.DefaultStyle.Font.Size = 8;
            fpSpread1.Sheets[0].DefaultStyle.Font.Name = "Verdana";
            fpSpread1.Sheets[0].DefaultStyle.Font.Size = 8;
            fpSpread1.Sheets[0].ColumnCount = 42;
            fpSpread1.Sheets[0].RemoveColumns(0, 42);
            fpSpread1.Sheets[0].ColumnCount = 42;

            string strDate = RiQi;

            //设置文字居中
            for (int i = 0; i < 42; i++)
            {
                fpSpread1.Sheets[0].Columns[i].HorizontalAlign = HorizontalAlign.Center;
                fpSpread1.Sheets[0].Columns[i].VerticalAlign = VerticalAlign.Middle;
            }

            ////Set up the headers
            fpSpread1.Sheets[0].ColumnHeader.RowCount = 1;
            //第一行列标题
            fpSpread1.Sheets[0].Cells[0, 0].ColumnSpan = 42;
            fpSpread1.Sheets[0].Cells[0, 0].Text = "单井射孔成本报表";
            fpSpread1.Sheets[0].Cells[0, 0].Font.Name = "Arial";
            fpSpread1.Sheets[0].Cells[0, 0].Font.Size = 16;
            fpSpread1.Sheets[0].Cells[0, 0].Font.Bold = true;
            fpSpread1.Sheets[0].Rows[0].Height = 40;

            //第二行列标题
            fpSpread1.Sheets[0].Rows[1].Height = 30;
            fpSpread1.Sheets[0].Cells[1, 0].Text = "序号";
            fpSpread1.Sheets[0].Cells[1, 1].Text = "施工方式";
            fpSpread1.Sheets[0].Cells[1, 2].Text = "类型";
            fpSpread1.Sheets[0].Cells[1, 3].Text = "计数";
            fpSpread1.Sheets[0].Cells[1, 4].Text = "项目";
            fpSpread1.Sheets[0].Cells[1, 5].Text = "弹型";
            fpSpread1.Sheets[0].Cells[1, 6].Text = "枪型";
            fpSpread1.Sheets[0].Cells[1, 7].Text = "周艳丽枪型";
            fpSpread1.Sheets[0].Cells[1, 8].Text = "复合类型";
            fpSpread1.Sheets[0].Cells[1, 9].Text = "井号";
            fpSpread1.Sheets[0].Cells[1, 10].Text = "结算类型";
            fpSpread1.Sheets[0].Cells[1, 11].Text = "单井收入";
            fpSpread1.Sheets[0].Cells[1, 12].Text = "射开厚度";
            fpSpread1.Sheets[0].Cells[1, 13].Text = "弹数(发)";
            fpSpread1.Sheets[0].Cells[1, 14].Text = "弹单价(元)";
            fpSpread1.Sheets[0].Cells[1, 15].Text = "弹金额";
            fpSpread1.Sheets[0].Cells[1, 16].Text = "弹金额(数值)";
            fpSpread1.Sheets[0].Cells[1, 17].Text = "枪长(米)";
            fpSpread1.Sheets[0].Cells[1, 18].Text = "枪单价(元/米)";
            fpSpread1.Sheets[0].Cells[1, 19].Text = "枪长2(米)";
            fpSpread1.Sheets[0].Cells[1, 20].Text = "枪单价2(元/米)";
            fpSpread1.Sheets[0].Cells[1, 21].Text = "枪金额";
            fpSpread1.Sheets[0].Cells[1, 22].Text = "枪金额(数值)";
            fpSpread1.Sheets[0].Cells[1, 23].Text = "夹层枪长";
            fpSpread1.Sheets[0].Cells[1, 24].Text = "夹层枪单价(元/米)";
            fpSpread1.Sheets[0].Cells[1, 25].Text = "夹层枪金额";
            fpSpread1.Sheets[0].Cells[1, 26].Text = "枪、弹合计金额(元)";
            fpSpread1.Sheets[0].Cells[1, 27].Text = "夹层枪金额(数值)";
            fpSpread1.Sheets[0].Cells[1, 28].Text = "爆炸器材单井消耗(元)";
            fpSpread1.Sheets[0].Cells[1, 29].Text = "一般材料单井消耗(元)";
            fpSpread1.Sheets[0].Cells[1, 30].Text = "平摊一般材料";
            fpSpread1.Sheets[0].Cells[1, 31].Text = "单井材料成本(元/口)";
            fpSpread1.Sheets[0].Cells[1, 32].Text = "固定费用";
            fpSpread1.Sheets[0].Cells[1, 33].Text = "折旧费";
            fpSpread1.Sheets[0].Cells[1, 34].Text = "其他变动费用";
            fpSpread1.Sheets[0].Cells[1, 35].Text = "单井成本";
            fpSpread1.Sheets[0].Cells[1, 36].Text = "盈余";
            fpSpread1.Sheets[0].Cells[1, 37].Text = "井段底深";
            fpSpread1.Sheets[0].Cells[1, 38].Text = "井段顶深";
            fpSpread1.Sheets[0].Cells[1, 39].Text = "井段";
            fpSpread1.Sheets[0].Cells[1, 40].Text = "井段/射开";
            fpSpread1.Sheets[0].Cells[1, 41].Text = "射开厚度";

            
            fpSpread1.Sheets[0].FrozenRowCount = 2  ;


            #endregion

        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            #region 赋值
            string strDate = txtSHRQ.Text.Trim();
            string sql = @"select * from tb_djskcb where sjrq=to_date('" + strDate + "','yyyy-mm-dd')  ";
            DataTable dt = new DataTable();
            dt = CommonDB.Query(sql).Tables[0];
            fpSpread1.Sheets[0].RowCount = dt.Rows.Count + 2;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 0].Text = dt.Rows[j]["ID"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 1].Text = dt.Rows[j]["SGFS"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 2].Text = dt.Rows[j]["LX"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 3].Text = dt.Rows[j]["JS"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 4].Text = dt.Rows[j]["XM"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 5].Text = dt.Rows[j]["DX"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 6].Text = dt.Rows[j]["QX"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 7].Text = dt.Rows[j]["ZYLQX"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 8].Text = dt.Rows[j]["FHLX"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 9].Text = dt.Rows[j]["jh"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 10].Text = dt.Rows[j]["JSLX"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 11].Text = dt.Rows[j]["DJSR"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 12].Text = dt.Rows[j]["SKHD"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 13].Text = dt.Rows[j]["DS"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 14].Text = dt.Rows[j]["DDJ"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 15].Text = dt.Rows[j]["DJE"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 16].Text = dt.Rows[j]["DJESZ"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 17].Text = dt.Rows[j]["QC"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 18].Text = dt.Rows[j]["QDJ"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 19].Text = dt.Rows[j]["QC2"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 20].Text = dt.Rows[j]["QDJ2"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 21].Text = dt.Rows[j]["QJE"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 22].Text = dt.Rows[j]["QJESZ"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 23].Text = dt.Rows[j]["JCQC"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 24].Text = dt.Rows[j]["JCQDJ"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 25].Text = dt.Rows[j]["JCQJE"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 26].Text = dt.Rows[j]["QDHJJE"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 27].Text = dt.Rows[j]["JCQJE"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 28].Text = dt.Rows[j]["BZQCDJXH"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 29].Text = dt.Rows[j]["YBCLDJXH"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 30].Text = dt.Rows[j]["PTYBCL"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 31].Text = dt.Rows[j]["DJCLCB"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 32].Text = dt.Rows[j]["GDFY"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 33].Text = dt.Rows[j]["ZJF"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 34].Text = dt.Rows[j]["QTBDFY"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 35].Text = dt.Rows[j]["DJCB"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 36].Text = dt.Rows[j]["YY"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 37].Text = dt.Rows[j]["JDDS"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 38].Text = dt.Rows[j]["JDDS1"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 39].Text = dt.Rows[j]["JD"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 40].Text = dt.Rows[j]["JDSK"].ToString();
                fpSpread1.Sheets[0].Cells[j + mTouRowNum, 41].Text = dt.Rows[j]["SKHD2"].ToString();
            }
            

            #endregion 赋值


        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            #region 打印
            try
            {
                FarPoint.Web.Spread.PrintInfo pi = new FarPoint.Web.Spread.PrintInfo();
                //pi = fpSpread1.Sheets[0].PrintInfo;
                //pi.ColStart = fpSpread1.ActiveSheet.Models.Selection.AnchorColumn;
                //pi.ColEnd = fpSpread1.ActiveSheet.Models.Selection.LeadColumn;
                //pi.RowStart = fpSpread1.ActiveSheet.Models.Selection.AnchorRow;
                //pi.RowEnd = fpSpread1.ActiveSheet.Models.Selection.LeadRow;
                pi.Orientation = FarPoint.Web.Spread.PrintOrientation.Landscape;//横向
                System.Drawing.Printing.PaperSize psize = new System.Drawing.Printing.PaperSize("A3", 1169, 1654);//297, 420毫米单位需要转换为英寸//1169,1654
                pi.PaperSize = psize;
                //pi.SmartPrintPagesTall = 1169;
                //pi.SmartPrintPagesWide = 2000;

                pi.PrintType = FarPoint.Web.Spread.PrintType.All; ;
                //pi.ShowColumnHeaders = false;           //显示列头
                //pi.ShowRowHeaders = false;              //显示行头
                pi.ShowGrid = true;                     //显示网格
                pi.ShowBorder = true;                   //显示边框

                //pi..Preview = true;                    //打印预览

                fpSpread1.Sheets[0].PrintInfo = pi;

                //fpSpread1.PrintSheet(0);

            }
            catch (Exception ex)
            {
                lblMsg.Text = lblMsg.Text + "\r\n" + ex.Message;
            }
            #endregion
        }

        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="filename"></param>
        public void SaveExcelFile(string filename)
        {
            #region

            bool ret;

            //Save
            try
            {
                //ret = fpSpread1.SaveExcel(filename);
                foreach (SheetView view in fpSpread1.Sheets)
                {
                    view.Protect = false;
                }
                ret = fpSpread1.SaveExcelToResponse(Server.UrlEncode(filename));
                if (ret == false)
                {
                    lblMsg.Text = lblMsg.Text + "\r\n" + "文件保存失败！";
                }
            }
            catch (System.Exception ex)
            {
                lblMsg.Text = lblMsg.Text + "\r\n" + ex.Message;
            }

            #endregion
        }
        protected void btnDCEXCEL_Click(object sender, EventArgs e)
        {
            #region 导出excel
            int rowNum = this.fpSpread1.Rows.Count;
            int colNum = this.fpSpread1.Columns.Count;
            int borderSize = 2;
            Border border2 = new Border();
            border2.BorderSizeTop = borderSize;
            border2.BorderSizeRight = borderSize;
            border2.BorderSizeBottom = borderSize;
            border2.BorderSizeLeft = borderSize;
            border2.BorderStyle = BorderStyle.Solid;
            border2.BorderColor = Color.Black;

            fpSpread1.Sheets[0].Cells[0, 0, rowNum - 1, colNum - 1].Border = border2;
            try
            {
                SaveExcelFile("单井射孔成本" + txtSHRQ.Text.Trim() + ".xls");
                fpSpread1.Sheets[0].Cells[0, 0, rowNum - 1, colNum - 1].ResetBorder();
            }
            catch (Exception ex)
            {
                lblMsg.Text = lblMsg.Text + "\r\n" + ex.Message;
            }

            #endregion
        }
    }
}